package org.example.Tools;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class VerificationCodeUtil {

    public BufferedImage createImage(StringBuilder codefinall) {
        long serialVersionUID = 1L;
        int WIDTH = 100;
        int HEIGHT = 40;
        int CODE_COUNT = 4;
        int FONT_HEIGHT = 30;
        char[] CODE_SEQUENCE = {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
        };
        // Create the image buffer
        BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

        // Fill background
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, WIDTH, HEIGHT);

        // Draw the border
        graphics.setColor(Color.BLACK);
        graphics.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);

        // Create font and set font style
        Font font = new Font("Fixedsys", Font.BOLD, FONT_HEIGHT);
        graphics.setFont(font);

        // Generate random code
        Random random = new Random();
        //codefinall= new StringBuilder(CODE_COUNT);
        for (int i = 0; i < CODE_COUNT; i++) {
            char code = CODE_SEQUENCE[random.nextInt(CODE_SEQUENCE.length)];
            codefinall.append(code);

            // Draw the character on the image
            graphics.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            graphics.drawString(String.valueOf(code),
                    (int)(i * 1.0 * WIDTH / CODE_COUNT),
                    (int)(HEIGHT / 1.5 + random.nextInt(10) - 5) );
        }
        graphics.dispose();
        return  bufferedImage;
    }
}
